﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace carsitem.Commodity
{
    public partial class CommodityMaintenanceForm : Form
    {
        //皮肤组件声明
        Sunisoft.IrisSkin.SkinEngine se = null;
        string SkinName = "SteelBlack";

        public CommodityMaintenanceForm()
        {
            InitializeComponent();

            //皮肤组件引用
            se = new Sunisoft.IrisSkin.SkinEngine();
            se.SkinAllForm = true;
            se.SkinFile = "skin/" + SkinName + ".ssk";
        }

        //打开窗口更新表格
        private void CommodityMaintenanceForm_Load(object sender, EventArgs e)
        {
            LoadCangKu();
            string sql = string.Format("select * from Commodity");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = dt;
        }

        //查找按钮
        private void Find_Click(object sender, EventArgs e)
        {
            try//捕捉全空文本框异常
            {
                //sql查找语句
                string sql = string.Format("select * from Commodity where GoodsNumber = '{0}'or CarName = '{1}'", this.textGoodsNumber.Text, this.textCarName.Text);
                
                //判断是否成功
                if (DBHelper.Instance().GetDataTableBySql(sql) != null)
                {
                    MessageBox.Show("查找成功！");
                    //成功后实时更新表格
                    string sql1 = string.Format("select * from Commodity");
                    DataTable dt = DBHelper.Instance().GetDataTableBySql(sql1);
                    this.dataGridView1.AutoGenerateColumns = false;
                    this.dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("查找失败！");
                }

                //清除文本框数据
                this.textGoodsNumber.Clear();
                this.textCarName.Clear();
            }
            catch (Exception)
            {
                MessageBox.Show("请输入要查找的数据！");//文本框中无数据弹出
            }
        }

        //刷新按钮
        private void Refresh_Click(object sender, EventArgs e)
        {
            //查询sql中数据，重新显示。
            string sql1 = string.Format("select * from Commodity");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql1);
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = dt;
        }

        //修改按钮
        private void Modify_Click(object sender, EventArgs e)
        {
            //连接修改窗口
            DBHelper db = new DBHelper();
            CommodityMaintenanceModifyForm commodityMaintenanceModifyForm = new CommodityMaintenanceModifyForm();
            commodityMaintenanceModifyForm.GoodsNumber = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value);
            commodityMaintenanceModifyForm.Owner = this;
            //弹出修改窗口
            commodityMaintenanceModifyForm.Show();

            this.Hide();//把自己隐藏了
        }

        //删除按钮
        private void Delete_Click(object sender, EventArgs e)
        {
            try//捕捉全空文本框异常
            {
                //sql删除语句
                string sql = string.Format("delete from Commodity where GoodsNumber = '{0}',GoodsName = '{1}'", this.textGoodsNumber.Text, this.textCarName.Text);
                if (DBHelper.Instance().ExcuteSql(sql))
                {
                    MessageBox.Show("删除成功！");

                    //成功后实时更新表格
                    string sql1 = string.Format("select * from Commodity");
                    DataTable dt = DBHelper.Instance().GetDataTableBySql(sql1);
                    this.dataGridView1.AutoGenerateColumns = false;
                    this.dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }

                //清除文本框数据
                this.textGoodsNumber.Clear();
                this.textCarName.Clear();
            }
            catch (Exception)
            {
                MessageBox.Show("请输入要删除的数据！");//文本框中无数据弹出
            }
        }

        //退出按钮
        private void Eext_Click(object sender, EventArgs e)
        {
            carsitem.MainInterface.MainInterfaceForm mainInterfaceForm = new carsitem.MainInterface.MainInterfaceForm();//把form1传给form2
            mainInterfaceForm.Show();
            this.Hide();
        }

        //数据库重连方法
        public void LoadCangKu()
        {
            string sql1 = string.Format("select * from Commodity");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql1);
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = dt;
        }

        //数字验证
        public bool IsNumberic(string oText)
        {
            string pattern = @"^\d+(\.\d)?$";
            if (oText != "")
            {
                if (!Regex.IsMatch(oText, pattern))
                    MessageBox.Show("请输入数字！");
            }
            return true;
        }

        //汉字验证
        public bool IsChineseCharacters(string oText)
        {
            string text = @"[\u4E00-\u9FA5]+$";
            if (oText != "")
            {
                if (!Regex.IsMatch(oText.ToString(), text))
                    MessageBox.Show("请输入汉字！");
            }
            return true;
        }

        //非空验证
        public bool Null(string oText)
        {
            if (oText == "")
            {
                MessageBox.Show("必须填写！");
            }
            return true;
        }

        //表单验证
        private void textGoodsNumber_Leave(object sender, EventArgs e)//商品编号
        {
            string oText = textGoodsNumber.Text.Trim();
            IsNumberic(oText);
            Null(oText);
        }

        private void textCarName_Leave(object sender, EventArgs e)//汽车名字
        {
            string oText = textCarName.Text.Trim();
            IsChineseCharacters(oText);
            Null(oText);
        }
    }
}
